package com.xiyili.osc.ui.news;

import java.util.List;

import com.xiyili.osc.R;
import net.oschina.app.bean.News;
import net.oschina.app.common.StringUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 新闻资讯Adapter类
 */
public class NewsListAdapter extends BaseAdapter {
	private final List<News> 					listItems;//数据集合
	private final LayoutInflater 				listContainer;//视图容器

	/**
	 * 实例化Adapter
	 * @param context
	 * @param data
	 * @param resource
	 */
	public NewsListAdapter(Context context, List<News> data) {
		this.listContainer = LayoutInflater.from(context);	//创建视图容器并设置上下文
		this.listItems = data;
	}
	
	public int getCount() {
		return listItems.size();
	}

	public Object getItem(int arg0) {
		return null;
	}

	public long getItemId(int arg0) {
		return 0;
	}
	
	/**
	 * ListView Item设置
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		//Log.d("method", "getView");
		
		//自定义视图
		ListItemViewHolder  holder = null;
		
		if (convertView == null) {
			//获取list_item布局文件的视图
			convertView = listContainer.inflate(R.layout.news_v2_list_item, null);
			
			holder = new ListItemViewHolder();
			//获取控件对象
			holder.title = (TextView)convertView.findViewById(R.id.news_listitem_title);
			holder.author = (TextView)convertView.findViewById(R.id.news_listitem_author);
			holder.count= (TextView)convertView.findViewById(R.id.news_listitem_commentCount);
			holder.date= (TextView)convertView.findViewById(R.id.news_listitem_date);
			holder.flag= (ImageView)convertView.findViewById(R.id.news_listitem_flag);
			
			//设置控件集到convertView
			convertView.setTag(holder);
		}else {
			holder = (ListItemViewHolder)convertView.getTag();
		}	
		
		//设置文字和图片
		News news = listItems.get(position);
		
		holder.title.setText(news.getTitle());
		holder.title.setTag(news);//设置隐藏参数(实体类)
		holder.author.setText(news.getAuthor());
		holder.date.setText(StringUtils.friendly_time(news.getPubDate()));
		holder.count.setText(news.getCommentCount()+"");
		if(StringUtils.isToday(news.getPubDate()))
			holder.flag.setVisibility(View.VISIBLE);
		else
			holder.flag.setVisibility(View.GONE);
		
		return convertView;
	}
	
	static class ListItemViewHolder{				//自定义控件集合  
        public TextView title;  
	    public TextView author;
	    public TextView date;  
	    public TextView count;
	    public ImageView flag;
 }  

}